//
//  NSString+Simple.m
//  AJBracelet
//
//  Created by zorro on 15/5/27.
//  Copyright (c) 2015年 zorro. All rights reserved.
//

#import "NSString+Simple.h"
#import <sys/utsname.h>
@implementation NSString (Simple)

// 注明文件不需要备份
- (BOOL)addSkipBackupAttributeToItem {
    if (self == nil) {
        return NO;;
    }
    
    NSString *ssss = self;
    
    if (ssss.length <= 0) {
        return NO;
    }
    
    NSURL *URL = [[NSURL alloc] initFileURLWithPath:ssss];
    assert([[NSFileManager defaultManager] fileExistsAtPath:[URL path]]);
    
    NSError *error  = nil;
    BOOL    success = [URL setResourceValue:[NSNumber numberWithBool:YES]
                                     forKey:NSURLIsExcludedFromBackupKey error:&error];
    if (!success) {
        NSLogD(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    
    return success;
}

// 获取字符串的区域大小
- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize {
    NSDictionary *attrs = @{NSFontAttributeName : font};
    
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

#pragma mark - Contains

// 字符串是否包含 piece
- (BOOL)contains:(NSString *)piece {
    return ([self rangeOfString:piece].location != NSNotFound);
}

- (id)parseJSONString {
    if (self) {
        NSData *JSONData    = [self dataUsingEncoding:NSUTF8StringEncoding];
        id     responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:nil];
        return responseJSON;
    }
    
    return nil;
}

- (NSString *)trim {
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (BOOL)isValid {
    if (self && ![self isEqualToString:@""]) {
        return YES;
    }
    
    return NO;
}

- (BOOL)isUserName {
    NSString    *regex = @"(^[A-Za-z0-9]{4,18}$)";
    NSPredicate *pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}

- (BOOL)isMotto {
    //^[A-Za-z0-9-_]{1,10}$
    // ^[A-Za-z0-9\u4e00-\u9fa5]{3,20}$
    NSString    *regex = @"(^[A-Za-z0-9\u4e00-\u9fa5]{3,25}$)";
    NSPredicate *pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}

- (BOOL)isNickName {
    //^[A-Za-z0-9-_]{1,10}$
    // ^[A-Za-z0-9\u4e00-\u9fa5]{3,20}$
    NSString    *regex = @"(^[A-Za-z0-9\u4e00-\u9fa5]{3,16}$)";
    NSPredicate *pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}

- (BOOL)isPassword {
    NSString    *regex = @"(^[A-Za-z0-9]{6,14}$)";
    NSPredicate *pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}

- (BOOL)isCode {
    NSString    *regex = @"(^[A-Za-z0-9]{8,14}$)";
    NSPredicate *pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}

- (BOOL)isNumber {
    NSString    *regex = @"(^[0-9]{0,11}?)";
    NSPredicate *pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:self];
}

- (BOOL)isEmail {
    NSString    *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    
    return [emailTest evaluateWithObject:self];
}

/**
 *  通过key设置value或者取value
 *
 *  @param key
 *
 *  @return
 */
- (id)getObjectValue {
    NSUserDefaults *UD = [NSUserDefaults standardUserDefaults];
    
    return [UD objectForKey:self];
}

- (NSInteger)getIntValue {
    NSUserDefaults *UD = [NSUserDefaults standardUserDefaults];
    
    return [UD integerForKey:self];
}

- (BOOL)getBOOLValue {
    NSUserDefaults *UD = [NSUserDefaults standardUserDefaults];
    
    return [UD boolForKey:self];
}

- (void)setObjectValue:(id)value {
    NSUserDefaults *UD = [NSUserDefaults standardUserDefaults];
    
    [UD setObject:value forKey:self];
    [UD synchronize];
}

- (void)setIntValue:(NSInteger)value {
    NSUserDefaults *UD = [NSUserDefaults standardUserDefaults];
    
    [UD setInteger:value forKey:self];
    [UD synchronize];
}

- (void)setBOOLValue:(BOOL)value {
    NSUserDefaults *UD = [NSUserDefaults standardUserDefaults];
    
    [UD setBool:value forKey:self];
    [UD synchronize];
}

+ (NSString *)iphoneType {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
    if ([platform isEqualToString:@"iPhone1,1"])  return @"iPhone 2G";
    if ([platform isEqualToString:@"iPhone1,2"])  return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])  return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])  return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,2"])  return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"])  return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"])  return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])  return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,2"])  return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,3"])  return @"iPhone 5c";
    if ([platform isEqualToString:@"iPhone5,4"])  return @"iPhone 5c";
    if ([platform isEqualToString:@"iPhone6,1"])  return @"iPhone 5s";
    if ([platform isEqualToString:@"iPhone6,2"])  return @"iPhone 5s";
    if ([platform isEqualToString:@"iPhone7,1"])  return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone7,2"])  return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone8,1"])  return @"iPhone 6s";
    if ([platform isEqualToString:@"iPhone8,2"])  return @"iPhone 6s Plus";
    if ([platform isEqualToString:@"iPhone8,4"])  return @"iPhone SE";
    if ([platform isEqualToString:@"iPhone9,1"])  return @"iPhone 7";
    if ([platform isEqualToString:@"iPhone9,2"])  return @"iPhone 7 Plus";
    if ([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
    if ([platform isEqualToString:@"iPhone10,4"]) return @"iPhone 8";
    if ([platform isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus";
    if ([platform isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus";
    if ([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X";
    if ([platform isEqualToString:@"iPhone10,6"]) return @"iPhone X";
    if ([platform isEqualToString:@"iPod1,1"])  return @"iPod Touch 1G";
    if ([platform isEqualToString:@"iPod2,1"])  return @"iPod Touch 2G";
    if ([platform isEqualToString:@"iPod3,1"])  return @"iPod Touch 3G";
    if ([platform isEqualToString:@"iPod4,1"])  return @"iPod Touch 4G";
    if ([platform isEqualToString:@"iPod5,1"])  return @"iPod Touch 5G";
    if ([platform isEqualToString:@"iPad1,1"])  return @"iPad 1G";
    if ([platform isEqualToString:@"iPad2,1"])  return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,2"])  return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,3"])  return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,4"])  return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,5"])  return @"iPad Mini 1G";
    if ([platform isEqualToString:@"iPad2,6"])  return @"iPad Mini 1G";
    if ([platform isEqualToString:@"iPad2,7"])  return @"iPad Mini 1G";
    if ([platform isEqualToString:@"iPad3,1"])  return @"iPad 3";
    if ([platform isEqualToString:@"iPad3,2"])  return @"iPad 3";
    if ([platform isEqualToString:@"iPad3,3"])  return @"iPad 3";
    if ([platform isEqualToString:@"iPad3,4"])  return @"iPad 4";
    if ([platform isEqualToString:@"iPad3,5"])  return @"iPad 4";
    if ([platform isEqualToString:@"iPad3,6"])  return @"iPad 4";
    if ([platform isEqualToString:@"iPad4,1"])  return @"iPad Air";
    if ([platform isEqualToString:@"iPad4,2"])  return @"iPad Air";
    if ([platform isEqualToString:@"iPad4,3"])  return @"iPad Air";
    if ([platform isEqualToString:@"iPad4,4"])  return @"iPad Mini 2G";
    if ([platform isEqualToString:@"iPad4,5"])  return @"iPad Mini 2G";
    if ([platform isEqualToString:@"iPad4,6"])  return @"iPad Mini 2G";
    if ([platform isEqualToString:@"iPad4,7"])  return @"iPad Mini 3";
    if ([platform isEqualToString:@"iPad4,8"])  return @"iPad Mini 3";
    if ([platform isEqualToString:@"iPad4,9"])  return @"iPad Mini 3";
    if ([platform isEqualToString:@"iPad5,1"])  return @"iPad Mini 4";
    if ([platform isEqualToString:@"iPad5,2"])  return @"iPad Mini 4";
    if ([platform isEqualToString:@"iPad5,3"])  return @"iPad Air 2";
    if ([platform isEqualToString:@"iPad5,4"])  return @"iPad Air 2";
    if ([platform isEqualToString:@"iPad6,3"])  return @"iPad Pro 9.7";
    if ([platform isEqualToString:@"iPad6,4"])  return @"iPad Pro 9.7";
    if ([platform isEqualToString:@"iPad6,7"])  return @"iPad Pro 12.9";
    if ([platform isEqualToString:@"iPad6,8"])  return @"iPad Pro 12.9";
    if ([platform isEqualToString:@"i386"])  return @"iPhone Simulator";
    if ([platform isEqualToString:@"x86_64"])  return @"iPhone Simulator";
    NSLog(@"platform>>>%@",platform);
    
    return platform;
}

+ (NSString *)subString:(NSString *)string withToIndex:(NSInteger)index
{
    NSString *s = string;
    if (index <= s.length) {
        s = [s substringToIndex:index];
    }
    return s;
}

@end

